package globalObjects;

import com.jme.bounding.BoundingBox;
import com.jme.input.KeyBindingManager;
import com.jme.input.KeyInput;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.scene.Node;
import com.jme.scene.shape.Box;

/**
 * A class for creating player instances. Most frequently used in the home. If
 * you want generic support for the player this would be the best way. This
 * class isn't for games which just need input though. It is represented by a
 * real player model.
 * 
 * @author steven sayers
 */
public class Player {
	Node playerNode = new Node();
	float modifier;
	
	/**
	 * Constructor for setting the movement modifier.
	 * @param modifer
	 */
	public Player(float inModifer){
		setModifier(inModifer);
	}
	
	/**
	 * Returns a node with the player in it
	 * @return playerNode
	 */
	public Node getPlayer(float xSize, float ySize, float zSize) {
		Box playerBox = new Box("The box", new Vector3f(), xSize,ySize,zSize);
		playerBox.setDefaultColor(ColorRGBA.pink);
		playerBox.setModelBound(new BoundingBox());
		playerBox.updateModelBound();
		playerNode.attachChild(playerBox);
		return playerNode;
	}
	
	/**
	 * Binds keys to the control system for the X cordinate
	 */
	public void initXControls(){
		KeyBindingManager.getKeyBindingManager().add("x++", KeyInput.KEY_K);
		KeyBindingManager.getKeyBindingManager().add("x--", KeyInput.KEY_H);
	}
	
	/**
	 * Binds keys to the control system for the Y cordinate
	 */
	public void initYControls(){
		KeyBindingManager.getKeyBindingManager().add("y++", KeyInput.KEY_SUBTRACT);
		KeyBindingManager.getKeyBindingManager().add("y--", KeyInput.KEY_ADD);
	}
	
	/**
	 * Binds keys to the control system for the Z cordinate
	 */
	public void initZControls(){
		KeyBindingManager.getKeyBindingManager().add("z++", KeyInput.KEY_J);
		KeyBindingManager.getKeyBindingManager().add("z--", KeyInput.KEY_U);
	}
	
	/**
	 * Handles all keys that have been initialized with the init x,y,z method.
	 * @param supportHeldKey , true = you can hold the key down to move, false = you must press the key multiple times.
	 */
	public void handleControls(float tpf){
		boolean supportHeldKey = true;
		//X++
        if (KeyBindingManager.getKeyBindingManager().isValidCommand("x++", supportHeldKey)) {
        	playerNode.getLocalTranslation().x += getModifier()*tpf;
        
        }
        //x--
        else if (KeyBindingManager.getKeyBindingManager().isValidCommand("x--", supportHeldKey)) {
        	playerNode.getLocalTranslation().x -= getModifier()*tpf;
        }
        
        //y++
        if (KeyBindingManager.getKeyBindingManager().isValidCommand("y++", supportHeldKey)) {
        	playerNode.getLocalTranslation().y += getModifier()*tpf;
        }
        //y--
        else if (KeyBindingManager.getKeyBindingManager().isValidCommand("y--", supportHeldKey)) {
        	playerNode.getLocalTranslation().y -= getModifier()*tpf;
        }
        
        //z++
        if (KeyBindingManager.getKeyBindingManager().isValidCommand("z++", supportHeldKey)) {
        	playerNode.getLocalTranslation().z += getModifier()*tpf;
        }
        //z--
        else if (KeyBindingManager.getKeyBindingManager().isValidCommand("z--", supportHeldKey)) {
        	playerNode.getLocalTranslation().z -= getModifier()*tpf;
        }
	}

	public float getModifier() {
		return modifier;
	}

	public void setModifier(float modifier) {
		this.modifier = modifier;
	}


}
